// pages/groupSpace/communitySquare/communitySquare.js
const app = getApp();
var util = require('../../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    CarGroupID: 0,
    HeadUrl: '', //群头像
    CarGroupName: '', //群名称
    CityName: '', //城市
    WxGroupTotal: 0, //微信群总人数
    WxGroupCount: 0, //微信群数量
    Notices: [], //通告

    isNoMore: false, //是否还有数据   
    isShowLoad: false, //是否显示底部loading组件
    comments: [], //内容列表
    pageIndex: 1, //页索引
    pageSize: 5, //页大小

    autoplay: true,
    vertical: true,
    interval: 5000,
    circular: true,
    tabList: [{
        text: '推荐',
        subList: ['推荐', '随笔', '游记', '求助', '心情']
      }
      // {
      //   text: '成员',
      //   subList: []
      // }
    ],
    currentTabIndex: 0, //主tab索引
    currentSubTabIndex: 0, //子tab索引
    isCard: true,
    isShowTabList: false, //是否显示tab子项
  },
  // methods: {

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      CarGroupID: options.CarGroupID
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let self = this;
    this.setData({
      //CarGroupID: carGroupID || 1,
      PageIndex: 1,
      isShowTabList: false
    });
    if (self.data.comments.length < 1) {
      self.getGroupsList();
    }
    if (util.isNullOrEmpty(self.data.CarGroupName)) {
      this.getGroupData();
    }
    // }
    this.getNotice();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let self = this;
    self.setData({
      isNoMore: false, //是否还有数据   
      isShowLoad: false, //是否显示底部loading组件
      comments: [], //内容列表
      pageIndex: 1, //页索引
    });
    self.getGroupsList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let self = this;
    // console.log('onReachBottom')
    if (self.data.isNoMore) {
      return;
    }
    self.data.pageIndex++;
    self.getGroupsList(); //加载更多
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 群主入群
   */
  connectManager: function () {
    wx.navigateTo({
      url: '/pages/groupSpace/joinGroupOwner/joinGroupOwner?CarGroupID=' + this.data.CarGroupID
    })
  },
  /**
   * 群详情
   */
  groupDetail: function () {
    wx.navigateTo({
      url: '/pages/groupSpace/profile/profile?CarGroupID=' + this.data.CarGroupID
    })
  },
  /**
   * 公告详情
   */
  noticeDetail: function (e) {
    let self = this;
    let id = e.currentTarget.dataset.id;
    let notices = self.data.Notices;
    for (let i = 0; i < notices.length; i++) {
      if (notices[i].NoticeID == id) {
        if (notices[i].GroupLink) {
          let url = notices[i].GroupLink;
          let query = '?webUrl=' + encodeURIComponent(url);
          wx.navigateTo({
            url: '/pages/webView/webView' + query,
          })
        } else {
          wx.navigateTo({
            url: '/pages/groupSpace/noticeDetail/noticeDetail?NoticeID=' + id,
          })
        }
      }
    }

  },
  /**
   * 获取数据
   */
  getGroupData: function () {
    let self = this;
    let url = app.globalData.apiRootUrl + "/CarGroup/GetCarGroup";

    app.httpRequest({
      url: url,
      data: {
        groupId: self.data.CarGroupID
      },
      success: function (res) {
        let rdata = res.data;
        self.setData({
          HeadUrl: rdata.data.LogoUrl, //群头像
          CarGroupName: rdata.data.CarGroupName, //群名称
          CityName: rdata.data.CityName, //城市
          WxGroupTotal: rdata.data.Total,
          WxGroupCount: rdata.data.WxGroupCount,
        });
      },
      fail: function (err) {
        console.log('获取第一页数据失败');
      }
    })
  },
  /**
   * 获取通告
   */
  getNotice: function () {
    let self = this;
    let url = app.globalData.apiRootUrl + "/Common/Notices";

    app.httpRequest({
      url: url,
      data: {
        carGroupId: self.data.CarGroupID
      },
      success: function (res) {
        console.log(res);
        let rdata = res.data;
        self.setData({
          Notices: rdata.data
        });
      },
      fail: function (err) {
        console.log('获取第一页数据失败');

      }
    })
  },
  /**
   * 获取群列表
   */
  getGroupsList: function () {
    let self = this;
    let url = app.globalData.apiRootUrl + "/GroupComment/GetCommentList";
    let type = self.data.currentSubTabIndex;
    let isHot = -1;

    let data = {
      PageSize: self.data.pageSize,
      PageIndex: self.data.pageIndex,
      OpenID: app.globalData.userInfo.OpenID,
      Section: type, //1：车趣杂谈 2：车友生活 3.丽人馆
      carGroupId: self.data.CarGroupID,
      IsHot: type == 0 ? 1 : -1 //0：今日推荐
    };

    self.setData({
      isShowLoad: true //显示底部loading组件
    });

    app.httpRequest({
      url: url,
      data: data,
      success: function (res) {
        //console.log('文章', res);
        let rdata = res.data;
        if (rdata.status == 0 && rdata.data.rows) {
          let comments = self.data.comments;
          for (let i = 0; i < rdata.data.rows.length; i++) {
            rdata.data.rows[i].CreateDate = util.formatDate(rdata.data.rows[i].CreateDate, "yyyy-MM-dd HH:mm");
            comments.push(rdata.data.rows[i]);
          }
          self.setData({
            comments: comments
          });
        }

        //如果返回的数据长度小于页大小，则说明没有数据了
        if (rdata.data.rows.length < self.data.pageSize) {
          self.setData({
            isShowLoad: true,
            isNoMore: true // true 没有数据了  false  还有数据
          });
        } else {
          self.setData({
            isShowLoad: false //今日推荐是否显示底部loading组件
          });
        }
      },
      fail: function (err) {
        console.log('获取第一页数据失败');
        self.setData({
          isShowLoad: false //今日推荐是否显示底部loading组件
        });
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    })

  },
  //顶部自定义字体点击
  fontTap: function () {
    wx.showToast({
      title: '没有消息',
      icon: 'none'
    });
  },
  /**
   * tab切换
   */
  tabTap: function (e) {
    let self = this;
    let index = e.currentTarget.dataset.tabindex;
    let tabList = self.data.tabList;
    if (tabList[index].subList.length > 0) {
      self.setData({
        isShowTabList: !self.data.isShowTabList
      })
    }
    self.setData({
      currentTabIndex: index
    })
  },
  /**
   * 子tab切换
   */
  subTabTap: function (e) {
    let self = this;
    let index = e.currentTarget.dataset.subindex;
    let tabList = self.data.tabList;
    tabList[self.data.currentTabIndex].text = tabList[self.data.currentTabIndex].subList[index];
    self.setData({
      currentSubTabIndex: index,
      isShowTabList: true,
      tabList: tabList
    });
    self.onPullDownRefresh();
  },
  /**
   * 添加帖子
   */
  addGroupComment: function () {
    let self = this;
    if (app.canAddComment(self.data.CarGroupID)) {
      wx.navigateTo({
        url: '/pages/groupSpace/publish/publish?CarGroupID=' + self.data.CarGroupID,
      });
    }
  },
  /**
   * 详情
   */
  commentDetail: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/groupSpace/comment/comment?GroupCommentID=' + id,
    })
  },
  //点赞
  likeTap(e) {
    let self = this;
    if (app.canAddComment(self.data.CarGroupID)) {
      let id = e.currentTarget.dataset.id;
      let list = self.data.comments;
      for (let i = 0; i < list.length; i++) {
        if (list[i].GroupCommentID == id) {
          if (list[i].isLike) {
            return;
          } else {
            list[i].PraiseCount++;
            list[i].isLike = true;
            self.addLike(id);
            break;
          }
        }
      }
      self.setData({
        comments: list
      });
    }
  },
  /**
   * 点赞统计
   */
  addLike: function (id) {
    let self = this;

    let url = app.globalData.apiRootUrl + "/WxUser/WxUserTrailRecord";
    app.httpRequest({
      url: url,
      method: "POST",
      data: {
        UserID: app.globalData.userInfo.UserID,
        TrailIdentity: id,
        TrailType: 1, //浏览类型(1:产品 2：名片 3：文章 4：趣味问答,5：小程序，6：小游戏)
        ActionType: 5, //操作类别(1：转发 2：浏览 3：答题 4：评论 5：点赞，6：取消点赞)
        OpenID: app.globalData.userInfo.OpenID,
        UnionID: app.globalData.userInfo.UnionID,
      },
      success: function (res) {}
    })
  },
  /**
   * 头部自定义返回事件
   */
  customGoBack: function () {
    let self = this;
    wx.switchTab({
      url: '/pages/groupSpace/groupSpace',
    })
  }
  // }
})